package light.head.controller.site;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.site.CategoryDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.model.Category;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 *	产品类型
 */
@Controller
@RequestMapping(Url.CATEGORY)
public class CategoryController extends SupporterController {
	private CategoryDAO cDAO;
	@RequestMapping(Url.INIT)
	public ModelAndView tree(HttpServletRequest request, HttpServletResponse response) {
		List<Category> list = cDAO.loadAll(cu_organCode(request));
		if (null == list) {
			return new ModelAndView(Url.CATEGORY_TREE);	
		}
		StringBuffer tree = new StringBuffer();
		for (Category o : list) {
			if (null != o) {
				tree.append("p.add('" + o.getCode() + "','-1','" + o.getName() + "');");
				addree(tree, o.getSonList());
			}
		}
		request.setAttribute(Parm.TREE, tree);
		return new ModelAndView(Url.CATEGORY_TREE);	
	}
	private void addree(StringBuffer tree, List<Category> list) {
		if (null == tree || null == list || list.size() == 0)
			return;
		for (Category o : list) {
			if (null != o) {
				tree.append("p.add('" + o.getCode() + "','" + o.getPc() + "','" + o.getName() + "');");
				addree(tree, o.getSonList());
			}
		}
	}
	@RequestMapping(Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) {
		String code = request.getParameter(Parm.CODE);
		String pc = request.getParameter(Parm.PC);
		if (null == code || Sys.NONE.equals(code)) {
			if (null != pc && !Sys.NONE.equals(pc)) {
				Category cg = cDAO.get(Integer.parseInt(pc));
				if (null != cg) {
					request.setAttribute(Parm.PC, pc);
					request.setAttribute(Parm.NAME, cg.getName());
				}
			}
			return new ModelAndView(Url.CATEGORY_EDIT);
		}

		Category ca = cDAO.get(Integer.parseInt(code));
		if (null != ca) {
			request.setAttribute(Parm.INFO, ca);
		}
		return new ModelAndView(Url.CATEGORY_EDIT);	
	}

	@RequestMapping(value = Url.SAVE)
	public void save(Category c, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if (c.getCode() != null) {
			Category cc = cDAO.get(c.getCode());
			if(cc != null){
				if(c.getName() != null && !cc.getName().equals(c.getName())){
					cc.setDeleted(false);
					cc.setName(c.getName());
					result = cDAO.upd(cc);
				}
			}
		} else {
			c.setCt(new Date());
			c.setCu(cu_code(request));
			c.setOc(cu_organCode(request));
			c.setDeleted(false);
			result = cDAO.add(c);
		}
		if (result == RESULT.SUCCESS) {
			cDAO.loadAll(cu_organCode(request));
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}

	@RequestMapping(value = Url.DELETE)
	public void delete(Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if (code != null) {
			Category c = cDAO.get(code);
			c.setDeleted(true);
			result = cDAO.upd(c);
		}
		if (result == RESULT.SUCCESS) {
			cDAO.loadAll(cu_organCode(request));
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(Url.AIDINIT)
	public ModelAndView aidTree(HttpServletRequest request, HttpServletResponse response) {
		
		request.setAttribute(Parm.ID, request.getParameter(Parm.ID));
		request.setAttribute(Parm.NAME, request.getParameter(Parm.NAME));
		
		List<Category> clist = cDAO.getAll(cu_organCode(request));
		if (null != clist) {
			StringBuffer tree = new StringBuffer();
			for (Category o : clist) {
				if (null != o && (null == o.getDeleted() || false == o.getDeleted())) {
					tree.append("s.add('" + o.getCode() + "','-1','" + o.getName() + "');");
					aidTree1(tree, o.getSonList());
				}
			}
			request.setAttribute(Parm.TREE, tree);
		}
		return new ModelAndView(Url.CATEGORY_AID_TREE);	
	}
	private void aidTree1(StringBuffer tree, List<Category> list) {
		if (null == tree || null == list || list.size() == 0)
			return;
		for (Category o : list) {
			if (null != o && (null == o.getDeleted() || false == o.getDeleted())) {
				tree.append("s.add('" + o.getCode() + "','" + o.getPc() + "','" + o.getName() + "');");
				aidTree1(tree, o.getSonList());
			}
		}
	}
	@Autowired
	public void setcDAO(CategoryDAO cDAO) {
		this.cDAO = cDAO;
	}
}
